ffd784
@@ -80,7 +80,7 @@
public SQLOperation(HiveSession parentSession, String statement, Map<String,
   public void prepare() throws HiveSQLException {
   }
 
-  private void runInternal() throws HiveSQLException {
+  private void runInternal(HiveConf sqlOperationConf) throws HiveSQLException {
     setState(OperationState.RUNNING);
     String statement_trimmed = statement.trim();
     String[] tokens = statement_trimmed.split("\\s");
@@ -91,13 +91,13 @@
private void runInternal() throws HiveSQLException {
     String SQLState = null;
 
     try {
-      driver = new Driver(getParentSession().getHiveConf(), getParentSession().getUserName());
+      driver = new Driver(sqlOperationConf, getParentSession().getUserName());
       // In Hive server mode, we are not able to retry in the FetchTask
       // case, when calling fetch queries since execute() has returned.
       // For now, we disable the test attempts.
       driver.setTryCount(Integer.MAX_VALUE);
 
-      String subStatement = new VariableSubstitution().substitute(getParentSession().getHiveConf(), statement);
+      String subStatement = new VariableSubstitution().substitute(sqlOperationConf, statement);
 
       response = driver.run(subStatement);
       if (0 != response.getResponseCode()) {
@@ -143,7 +143,7 @@
private void runInternal() throws HiveSQLException {
   public void run() throws HiveSQLException {
     setState(OperationState.PENDING);
     if (!shouldRunAsync()) {
-      runInternal();
+      runInternal(getConfigForOperation());
     } else {
       Runnable backgroundOperation = new Runnable() {
         SessionState ss = SessionState.get();
@@ -151,7 +151,7 @@
public void run() throws HiveSQLException {
         public void run() {
           SessionState.start(ss);
           try {
-            runInternal();
+            runInternal(getConfigForOperation());
           } catch (HiveSQLException e) {
             LOG.error("Error: ", e);
             // TODO: Return a more detailed error to the client,
@@ -318,4 +318,32 @@
private boolean shouldRunAsync() {
     return runAsync;
   }
 
+  /**
+   * If there are query specific settings to overlay, then create a copy of config
+   * There are two cases we need to clone the session config that's being passed to hive driver
+   * 1. Async query -
+   *    If the client changes a config setting, that shouldn't reflect in the execution already underway
+   * 2. confOverlay -
+   *    The query specific settings should only be applied to the query config and not session
+   * @return new configuration
+   * @throws HiveSQLException
+   */
+  private HiveConf getConfigForOperation() throws HiveSQLException {
+    HiveConf sqlOperationConf = getParentSession().getHiveConf();
+    if (!getConfOverlay().isEmpty() || shouldRunAsync()) {
+      // clone the partent session config for this query
+      sqlOperationConf = new HiveConf(sqlOperationConf);
+
+      // apply overlay query specific settings, if any
+      for (Map.Entry<String, String> confEntry : getConfOverlay().entrySet()) {
+        try {
+          sqlOperationConf.verifyAndSet(confEntry.getKey(), confEntry.getValue());
+        } catch (IllegalArgumentException e) {
+          throw new HiveSQLException("Error applying statement specific settings", e);
+        }
+      }
+    }
+    return sqlOperationConf;
+  }
+
 }
